package data.join.com.network;

import com.google.gson.Gson;

import data.join.com.utils.MyLog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/4/12.
 */

public abstract class DefaultCallBack<T> implements Callback<T> {
    public String TAG = "DefaultCallBack";
    int retryCount = 0;

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if(response.body() != null) {
            onCallResponse(call, response, response.body());
        }else{
            onFailure(call, new Throwable("errorBody:empty"));
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        if(t == null) t = new Throwable("body is null");

        MyLog.D(TAG,"onFailure:"+t.toString());

        if(t.getMessage() == null) t = new Throwable("empty message");

        if(t.toString().contains("Canceled")){

        }else if(t.toString().contains("Socket closed")){

        }else {
            onCallFailure(call, t);
        }
    }

    abstract public void onCallResponse(Call<T> call,Response<T> response,T t);

    abstract public void onCallFailure(Call<T> call, Throwable t);

}
